const state = {
  backVisitedViews: [],
  backCachedViews: []
}

const mutations = {
  ADD_VISITED_VIEW: (state, view) => {
    if (state.backVisitedViews.some(v => (v.fullPath+v.query) === (view.fullPath+v.query))) return
    state.backVisitedViews.push(
      Object.assign({}, view, {
        title: view.meta.title || 'no-name'
      })
    )
  },
  ADD_CACHED_VIEW: (state, view) => {
    if (state.backCachedViews.includes(view.fullPath+view.query)) return
    if (!view.meta.noCache) {
      state.backCachedViews.push(view.fullPath+view.query)
    }
  },

  DEL_VISITED_VIEW: (state, view) => {
    for (const [i, v] of state.backVisitedViews.entries()) {
      if (v.fullPath === view.fullPath) {
        state.backVisitedViews.splice(i, 1)
        break
      }
    }
  },
  DEL_CACHED_VIEW: (state, view) => {
    for (const i of state.backCachedViews) {
      if (i === view.fullPath) {
        const index = state.backCachedViews.indexOf(i)
        state.backCachedViews.splice(index, 1)
        break
      }
    }
  },

  DEL_OTHERS_VISITED_VIEWS: (state, view) => {
    state.backVisitedViews = state.backVisitedViews.filter(v => {
      return v.meta.affix || v.fullPath === view.fullPath
    })
  },

  DEL_LEFT_VISITED_VIEWS: (state, view) => {
    let flag = false
    state.backVisitedViews = state.backVisitedViews.filter(v => {
      if (v.fullPath === view.fullPath) {
        flag = true
      }
      return v.meta.affix || (flag || v.fullPath === view.fullPath)
    })
  },

  DEL_RIGHT_VISITED_VIEWS: (state, view) => {
    let flag = false
    state.backVisitedViews = state.backVisitedViews.filter(v => {
      if (v.fullPath === view.fullPath) {
        flag = true
      }
      return v.meta.affix || (!flag || v.fullPath === view.fullPath)
    })
  },
  DEL_LEFT_CACHED_VIEWS: (state, view) => {
    for (const i of state.backCachedViews) {
      if (i === view.fullPath) {
        const index = state.backCachedViews.indexOf(i)
        state.backCachedViews = state.backCachedViews.slice(index, state.backCachedViews.length)
        break
      }
    }
  },
  DEL_RIGHT_CACHED_VIEWS: (state, view) => {
    for (const i of state.backCachedViews) {
      if (i === view.fullPath) {
        const index = state.backCachedViews.indexOf(i)
        state.backCachedViews = state.backCachedViews.slice(0,index)
        break
      }
    }
  },
  DEL_OTHERS_CACHED_VIEWS: (state, view) => {
    for (const i of state.backCachedViews) {
      if (i === view.fullPath) {
        const index = state.backCachedViews.indexOf(i)
        state.backCachedViews = state.backCachedViews.slice(index, index + 1)
        break
      }
    }
  },

  DEL_ALL_VISITED_VIEWS: state => {
    // keep affix tags
    const affixTags = state.backVisitedViews.filter(tag => tag.meta.affix)
    state.backVisitedViews = affixTags
  },
  DEL_ALL_CACHED_VIEWS: state => {
    state.backCachedViews = []
  },

  UPDATE_VISITED_VIEW: (state, view) => {
    for (let v of state.backVisitedViews) {
      if (v.fullPath === view.fullPath) {
        v = Object.assign(v, view)
        break
      }
    }
  }
}

const actions = {
  addView({ dispatch }, view) {
    dispatch('addVisitedView', view)
    dispatch('addCachedView', view)
  },
  addVisitedView({ commit }, view) {
    commit('ADD_VISITED_VIEW', view)
  },
  addCachedView({ commit }, view) {
    commit('ADD_CACHED_VIEW', view)
  },

  delView({ dispatch, state }, view) {
    return new Promise(resolve => {
      dispatch('delVisitedView', view)
      dispatch('delCachedView', view)
      resolve({
        backVisitedViews: [...state.backVisitedViews],
        backCachedViews: [...state.backCachedViews]
      })
    })
  },
  delVisitedView({ commit, state }, view) {
    return new Promise(resolve => {
      commit('DEL_VISITED_VIEW', view)
      resolve([...state.backVisitedViews])
    })
  },
  delCachedView({ commit, state }, view) {
    return new Promise(resolve => {
      commit('DEL_CACHED_VIEW', view)
      resolve([...state.backCachedViews])
    })
  },

  delOthersViews({ dispatch, state }, view) {
    return new Promise(resolve => {
      dispatch('delOthersVisitedViews', view)
      dispatch('delOthersCachedViews', view)
      resolve({
        backVisitedViews: [...state.backVisitedViews],
        backCachedViews: [...state.backCachedViews]
      })
    })
  },
  delOthersVisitedViews({ commit, state }, view) {
    return new Promise(resolve => {
      commit('DEL_OTHERS_VISITED_VIEWS', view)
      resolve([...state.backVisitedViews])
    })
  },
  delLeftViews({ dispatch, state }, view) {
    return new Promise(resolve => {
      dispatch('delLeftVisitedViews', view)
      dispatch('delLeftCachedViews', view)
      resolve({
        backVisitedViews: [...state.backVisitedViews],
        backCachedViews: [...state.backCachedViews]
      })
    })
  },
  delLeftVisitedViews({ commit, state }, view) {
    return new Promise(resolve => {
      commit('DEL_LEFT_VISITED_VIEWS', view)
      resolve([...state.backVisitedViews])
    })
  },
  delRightViews({ dispatch, state }, view) {
    return new Promise(resolve => {
      dispatch('delRightVisitedViews', view)
      dispatch('delRightCachedViews', view)
      resolve({
        backVisitedViews: [...state.backVisitedViews],
        backCachedViews: [...state.backCachedViews]
      })
    })
  },
  delRightVisitedViews({ commit, state }, view) {
    return new Promise(resolve => {
      commit('DEL_RIGHT_VISITED_VIEWS', view)
      resolve([...state.backVisitedViews])
    })
  },

  delLeftCachedViews({ commit, state }, view) {
    return new Promise(resolve => {
      commit('DEL_LEFT_CACHED_VIEWS', view)
      resolve([...state.backCachedViews])
    })
  },
  delRightCachedViews({ commit, state }, view) {
    return new Promise(resolve => {
      commit('DEL_RIGHT_CACHED_VIEWS', view)
      resolve([...state.backCachedViews])
    })
  },
  delOthersCachedViews({ commit, state }, view) {
    return new Promise(resolve => {
      commit('DEL_OTHERS_CACHED_VIEWS', view)
      resolve([...state.backCachedViews])
    })
  },

  delAllViews({ dispatch, state }, view) {
    return new Promise(resolve => {
      dispatch('delAllVisitedViews', view)
      dispatch('delAllCachedViews', view)
      resolve({
        backVisitedViews: [...state.backVisitedViews],
        backCachedViews: [...state.backCachedViews]
      })
    })
  },
  delAllVisitedViews({ commit, state }) {
    return new Promise(resolve => {
      commit('DEL_ALL_VISITED_VIEWS')
      resolve([...state.backVisitedViews])
    })
  },
  delAllCachedViews({ commit, state }) {
    return new Promise(resolve => {
      commit('DEL_ALL_CACHED_VIEWS')
      resolve([...state.backCachedViews])
    })
  },

  updateVisitedView({ commit }, view) {
    commit('UPDATE_VISITED_VIEW', view)
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}
